package email;

import java.util.Comparator;

import email.DateObject;

public class ReceivedMessageComparator implements Comparator<ReceivedMessage> {

	@Override
	public int compare(ReceivedMessage message1, ReceivedMessage message2) {
		// Sun Nov 13 20:12:12 CST 2011
		DateObject date1 = new DateObject(message1);
		
		int monthNo1 = date1.getMonth();
		int dayNo1 = date1.getDay();
		int hoursNo1 = date1.getHours();
		int minutesNo1 = date1.getMinutes();
		int secondsNo1 = date1.getSeconds();
		int yearNo1 = date1.getYear();
		
		DateObject date2 = new DateObject(message2);
		int monthNo2 = date2.getMonth();
		int dayNo2 = date2.getDay();
		int hoursNo2 = date2.getHours();
		int minutesNo2 = date2.getMinutes();
		int secondsNo2 = date2.getSeconds();
		int yearNo2 = date2.getYear();
		
		if(yearNo1 > yearNo2){
			return 1;
		}else if(yearNo1 < yearNo2){
			return -1;
		}else{
			if(monthNo1 > monthNo2){
				return 1;
			}else if(monthNo1 < monthNo2){
				return -1;
			}else{
				if(dayNo1 > dayNo2){
					return 1;
				}else if(dayNo1 < dayNo2){
					return -1;
				}else{
					if(hoursNo1 > hoursNo2){
						return 1;
					}else if(hoursNo1 < hoursNo2){
						return -1;
					}else{
						if(minutesNo1 > minutesNo2){
							return 1;
						}else if(minutesNo1 < minutesNo2){
							return -1;
						}else{
							if(secondsNo1 > secondsNo2){
								return 1;
							}else if(secondsNo1 < secondsNo2){
								return -1;
							}else{
								return 0;
							}
						}
					}
				}
			}
		}
	}
	
	
	public static Comparator<ReceivedMessage> reverseOrder(){
		return new Comparator<ReceivedMessage>() {
			@Override
			public int compare(ReceivedMessage message1, ReceivedMessage message2) {
				DateObject date1 = new DateObject(message1);
				
				int monthNo1 = date1.getMonth();
				int dayNo1 = date1.getDay();
				int hoursNo1 = date1.getHours();
				int minutesNo1 = date1.getMinutes();
				int secondsNo1 = date1.getSeconds();
				int yearNo1 = date1.getYear();
				
				DateObject date2 = new DateObject(message2);
				int monthNo2 = date2.getMonth();
				int dayNo2 = date2.getDay();
				int hoursNo2 = date2.getHours();
				int minutesNo2 = date2.getMinutes();
				int secondsNo2 = date2.getSeconds();
				int yearNo2 = date2.getYear();
				
				if(yearNo1 < yearNo2){
					return 1;
				}else if(yearNo1 > yearNo2){
					return -1;
				}else{
					if(monthNo1 < monthNo2){
						return 1;
					}else if(monthNo1 > monthNo2){
						return -1;
					}else{
						if(dayNo1 < dayNo2){
							return 1;
						}else if(dayNo1 > dayNo2){
							return -1;
						}else{
							if(hoursNo1 < hoursNo2){
								return 1;
							}else if(hoursNo1 > hoursNo2){
								return -1;
							}else{
								if(minutesNo1 < minutesNo2){
									return 1;
								}else if(minutesNo1 > minutesNo2){
									return -1;
								}else{
									if(secondsNo1 < secondsNo2){
										return 1;
									}else if(secondsNo1 > secondsNo2){
										return -1;
									}else{
										return 0;
									}
								}
							}
						}
					}
				}
			}
		};
	}

}
